﻿using System.IO;
using System.Xml.Serialization;
//Highscore source http://xnaessentials.com/tutorials/highscores.aspx

namespace HindernisGame
{

    public struct HighScoreData
    {
        public string[] PlayerName;
        public int[] Score;
        public int Count;

        public HighScoreData(int count)
        {
            PlayerName = new string[count];
            Score = new int[count];
            Count = count;
        }
    }
    public class Highscore
    {
        private static string HighScoresFilename = "hs.dat";       

        public static void SaveHighScores(HighScoreData data, string filename)
        {
            string fullpath = Path.Combine(filename);
            FileStream stream = File.Open(fullpath, FileMode.Create);
            try
            {
                // Convert the object to XML data and put it in the stream
                XmlSerializer serializer = new XmlSerializer(typeof(HighScoreData));
                serializer.Serialize(stream, data);
            }
            finally
            {
                stream.Close();
            }
        }

        public HighScoreData LoadHighScores(string filename)
        {
            HighScoreData data;
            string fullpath = Path.Combine(filename);
            FileStream stream = File.Open(fullpath, FileMode.OpenOrCreate, FileAccess.Read);

            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(HighScoreData));
                data = (HighScoreData)serializer.Deserialize(stream);
            }
            finally
            {
                stream.Close();
            }
 
            return (data);
        }

        public void Initialize()
        {
            string fullpath = Path.Combine(HighScoresFilename);
            if (!File.Exists(fullpath))
            {
                HighScoreData data = new HighScoreData(10);
                data.PlayerName[0] = "Hugo";
                data.Score[0] = 10000;
                data.PlayerName[1] = "Hugo";
                data.Score[1] = 9000;
                data.PlayerName[2] = "Hugo";
                data.Score[2] = 8000;
                data.PlayerName[3] = "Hugo";
                data.Score[3] = 7000;
                data.PlayerName[4] = "Hugo";
                data.Score[4] = 6000;
                data.PlayerName[5] = "Hugo";
                data.Score[5] = 5000;
                data.PlayerName[6] = "Hugo";
                data.Score[6] = 4000;
                data.PlayerName[7] = "Hugo";
                data.Score[7] = 3000;
                data.PlayerName[8] = "Hugo";
                data.Score[8] = 2000;
                data.PlayerName[9] = "Hugo";
                data.Score[9] = 1000;

                SaveHighScores(data, HighScoresFilename);
            }
          
        }

        public void SaveHighScore(int score, string name)
        {
            // Create the data to save
            HighScoreData data = LoadHighScores(HighScoresFilename);
 
            int scoreIndex = -1;
            for (int i = 0; i < data.Count; i++)
            {
                if (score > data.Score[i])
                {
                    scoreIndex = i;
                    break;
                }
            }
 
            if (scoreIndex > -1)
            {
                //New high score found ... do swaps
                for (int i = data.Count - 1; i > scoreIndex; i--)
                {
                    data.PlayerName[i] = data.PlayerName[i - 1];
                    data.Score[i] = data.Score[i - 1];
                }
                data.PlayerName[scoreIndex] = name;
                data.Score[scoreIndex] = score;
 
                SaveHighScores(data, HighScoresFilename);
            }
        }
    }
}
